<?php

class Pager {

    private $model_obj = null;
    public $pageData = null;
    private $input_args = null;
    private $plus = 2;

    public function __input( $obj, $args ) {
        $this->model_obj = $obj;
        $this->input_args = $args;
        return $this;
    }

    /**
     * 魔术函数，支持多重函数式使用类的方法
     */
    public function __call( $func_name, $func_args ) {
        if ( 'findAll' == $func_name || 'findSql' == $func_name ) {
            return $this->runpager( $func_name, $func_args );
        } elseif ( method_exists( $this, $func_name ) ) {
            return call_user_func_array( array( $this, $func_name ), $func_args );
        } else {
            return call_user_func_array( array( $this->model_obj, $func_name ), $func_args );
        }
    }

    /**
     * 得到当前连接
     * @param $page
     * @param $text
     * @return string
     */
    protected function _get_link( $page, $text ) {
        $c = R( 'c' );
        $a = ( R( 'a' ) == 'index' or  !R( 'a' ) ) ? null : R( 'a' );
        $args = $_GET;
        unset( $args[ 'c' ] );
        unset( $args[ 'a' ] );
        $args[ 'page' ] = $page;
        $url = $this->model_obj->url( $c, $a, $args );
        return "<a href='{$url}'>{$text}</a>";
    }

    /**
     * 得到第一页
     * @return string
     */
    public function first_page( $name = '第一页' ) {
        if ( $this->pageData[ 'current_page' ] > 1 ) {
            return $this->_get_link( '1', $name );
        }
        return '';
    }

    /**
     * 最后一页
     * @param $name
     * @return string
     */
    public function last_page( $name = '最后一页' ) {
        if ( $this->pageData[ 'current_page' ] < $this->pageData[ 'total_page' ] ) {
            return $this->_get_link( $this->pageData[ 'total_page' ], $name );
        }
        return '';
    }

    /**
     * 上一页
     * @return string
     */
    public function up_page( $name = '上一页' ) {
        if ( $this->pageData[ 'current_page' ] != 1 ) {
            return $this->_get_link( $this->pageData[ 'current_page' ] - 1, $name );
        }
        return '';
    }

    /**
     * 下一页
     * @return string
     */
    public function down_page( $name = '下一页' ) {
        if ( $this->pageData[ 'current_page' ] < $this->pageData[ 'total_page' ] ) {
            return $this->_get_link( $this->pageData[ 'current_page' ] + 1, $name );
        }
        return '';
    }

    /**
     * 总记录数
     * @return string
     */
    public function total_count() {
        return '<span>总共' . $this->pageData[ 'total_count' ] . '条</span>';
    }

    /**
     * 分页样式输出
     * @param $param
     * @return string
     */
    public function showPage() {
        if ( $this->pageData ) {
            $plus = $this->plus;

            if ( $plus + $this->pageData[ 'current_page' ] > $this->pageData[ 'total_page' ] ) {
                $begin = $this->pageData[ 'total_page' ] - $plus * 2;
            } else {
                $begin = $this->pageData[ 'current_page' ] - $plus;
            }


            $begin = ( $begin >= 1 ) ? $begin : 1;
            $return = '';
            $return .= $this->first_page();
            $return .= $this->up_page();
            for ( $i = $begin; $i <= $begin + $plus * 2; $i++ ) {
                if ( $i > $this->pageData[ 'total_page' ] ) {
                    break;
                }
                if ( $i == $this->pageData[ 'current_page' ] ) {
                    $return .= "<a class='now_page'>$i</a>";
                } else {
                    $return .= $this->_get_link( $i, $i );
                }
            }
            $return .= $this->down_page();
            $return .= $this->last_page();
            $return .= $this->total_count();
        } else {
            $return = '';
        }
        return $return;
    }

    public function runpager( $func_name, $func_args ) {

        $this->pageData = null;
        $page = R( 'page' );
        $pageSize = isset( $this->input_args[ 0 ] ) ? $this->input_args[ 0 ] : 10;
        $limit = null;

        @list( $conditions, $sort, $fields ) = $func_args;

        if ( 'findSql' == $func_name ) {
            $total_count = array_pop( array_pop( $this->model_obj->model->findSql( "SELECT COUNT({$this->model_obj->model->pk}) as counter FROM ($conditions) tmp_table_pager" ) ) );
        } else {
            $total_count = $this->model_obj->model->findCount( $conditions );
        }

        if ( $total_count > $pageSize ) {
            $total_page = ceil( $total_count / $pageSize );
            $page = min( intval( max( $page, 1 ) ), $total_count ); // 对页码进行规范运算
            $this->pageData = array(
                "total_count" => $total_count, // 总记录数
                "page_size" => $pageSize, // 分页大小
                "total_page" => $total_page, // 总页数
                "first_page" => 1, // 第一页
                "prev_page" => ( ( 1 == $page ) ? 1 : ( $page - 1 ) ), // 上一页
                "next_page" => ( ( $page == $total_page ) ? $total_page : ( $page + 1 ) ), // 下一页
                "last_page" => $total_page, // 最后一页
                "current_page" => $page, // 当前页
                "all_pages" => array() // 全部页码
            );
            for ( $i = 1; $i <= $total_page; $i++ ) $this->pageData[ 'all_pages' ][ ] = $i;
            $limit = ( $page - 1 ) * $pageSize . "," . $pageSize;
            if ( 'findSql' == $func_name ) $conditions = $this->model_obj->model->setlimit( $conditions, $limit );
        }

        if ( 'findSql' == $func_name ) {
            $date[ 'result' ] = $this->model_obj->model->findSql( $conditions );
        } else {
            $date[ 'result' ] = $this->model_obj->model->findAll( $conditions, $sort, $fields, $limit );
        }
        $date[ 'page' ] = $this->showPage();
        return $date;
    }
}